package com.dhecp.project.system.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.project.system.domain.SysDictionaryRelationForField;
import com.dhecp.project.system.mapper.SysDictionaryRelationForFieldMapper;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import com.dhecp.project.system.service.SysDictionaryRelationForFieldService;

/**
 * 调入字段信息 业务实现层
 * @author DH-lwl
 * @version 2019年11月26日
 */
@Service
public class SysDictionaryRelationForFieldServiceImpl implements SysDictionaryRelationForFieldService {

	@Autowired
	private SysDictionaryRelationForFieldMapper dictionaryRelationForFieldMapper;
	
	@Autowired
	private SysDictionaryForFieldService dictionaryForFieldService;

	/**
	 * 查询所有调入字段数据
	 * @param params
	 * @return List-DictionaryRelationForField
	 * @author lwl
	 * @throws Exception 
	 */
	@Override
	public List<SysDictionaryRelationForField> queryDictionaryRelationForField(Map<String, Object> params) throws Exception {
		params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));
		PageUtils.startPage();
		return dictionaryRelationForFieldMapper.findDictionaryRelationForField(params);
	}

	/**
	 * 根据guid查询调入字段
	 * @param params
	 * @return 实体SysDictionaryRelationForField
	 * @author lwl
	 */
	@Override
	public SysDictionaryRelationForField queryDictionaryRelationForFieldByGuid(String guid) {
		return dictionaryRelationForFieldMapper.findDictionaryRelationForFieldByGuid(guid);
	}

	/**
	 * 通过pguid查询调入关系
	 * @param pguid
	 * @return List - SysDictionaryRelationForField
	 * @author DH-Xxj
	 */
	@Override
	public List<SysDictionaryRelationForField> queryDictionaryRelationForFieldByPguid(String pguid) {
		return dictionaryRelationForFieldMapper.findDictionaryRelationForFieldByPguid(pguid);
	}
	
	/**
	 * 新增调入字段
	 * @param dictionaryRelationForFieldItems
	 * @return MessageModel
	 * @author lwl
	 */
	@Override
	public AjaxResult insertDictionaryRelationForField(Map<String, Object> params) {
		List<SysDictionaryRelationForField> dictionaryRelationForFieldList = JSON.parseArray(params.get("dictionaryRelationForFieldItems").toString(), SysDictionaryRelationForField.class);
        if (dictionaryRelationForFieldList == null || dictionaryRelationForFieldList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
        for (SysDictionaryRelationForField dictionaryRelationForField : dictionaryRelationForFieldList) {
        	dictionaryRelationForFieldMapper.insertDictionaryRelationForField(dictionaryRelationForField);
        }
        return AjaxResult.success("操作成功");
	}

	/**
	 * 删除调入字段
	 * @param dictionaryRelationForFieldItems
	 * @return MessageModel
	 * @author lwl
	 */
	@Override
	public AjaxResult deleteDictionaryRelationForField(Map<String, Object> params) {
		List<SysDictionaryRelationForField> dictionaryRelationForFieldList = JSON.parseArray(params.get("dictionaryRelationForFieldItems").toString(), SysDictionaryRelationForField.class);
		if (dictionaryRelationForFieldList == null || dictionaryRelationForFieldList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (SysDictionaryRelationForField dictionaryRelationForField : dictionaryRelationForFieldList) {
			dictionaryRelationForFieldMapper.deleteDictionaryRelationForFieldByGuid(dictionaryRelationForField);
		}
		return AjaxResult.success("操作成功");
	}

	/**
	 * 修改调入字段
	 * @param dictionaryRelationForFieldItems
	 * @return MessageModel
	 * @author DH-lwl
	 */
	@Override
	public AjaxResult updateDictionaryRelationForField(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<SysDictionaryRelationForField> dictionaryRelationForFieldList = JSON.parseArray(params.get("dictionaryRelationForFieldItems").toString(), SysDictionaryRelationForField.class);
		if (dictionaryRelationForFieldList == null || dictionaryRelationForFieldList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
		for (SysDictionaryRelationForField dictionaryRelationForField : dictionaryRelationForFieldList) {
			dictionaryRelationForFieldMapper.updateDictionaryRelationForFieldByGuid(dictionaryRelationForField);
		}
		return AjaxResult.success("操作成功！");
	}
	
}
